<template>
  <!-- 企业套餐类型 -->
  <div class="app-container">
    <div class="form-container">
      <el-form
        :inline="true"
        label-width="80px"
        :model="filters"
        class="selectFrom"
      >
        <el-form-item label="类型名称" prop="name">
          <el-input
            v-model="filters.name"
            size="small"
            placeholder="类型名称"
            clearable
          />
        </el-form-item>
        <el-form-item label="类型状态" prop="status">
          <el-select
            v-model="filters.status"
            size="small"
            filterable
            placeholder="请选择"
            prop="status"
          >
            <el-option
              v-for="dict in statusOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="创建时间">
          <el-date-picker
            v-model="dateRange"
            size="small"
            value-format="yyyy-MM-dd"
            type="daterange"
            range-separator="-"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            :clearable="false"
          />
        </el-form-item>
      </el-form>
    </div>
    <div class="btns-group">
      <systemBtns :single="single" has-permi="sysCompanyTempType" />
      <el-button v-hasPermi="[`system:sysCompanyTempType:disable`]" size="middle" :icon="disabledStatus === '1'?'el-icon-star-on':'el-icon-star-off'" :disabled="single" @click="handleStatusChange">
        {{ disabledContent }}
      </el-button>
    </div>
    <el-row>
      <el-col :span="24">
        <!-- 表格展示start -->
        <el-table
          ref="multipleTable"
          v-loading="tableLoading"
          stripe
          class="table-list"
          :class="highLevelSet===true ? (needTagsView===true ? 'el-body-page-roll-tag-height' : 'el-body-page-roll-height') :
            (needTagsView===true ? 'el-body-page-roll-tag' : 'el-body-page-roll')"
          :data="tableData"
          :row-style="rowClass"
          @selection-change="handleSelectionChange"
          @row-click="rowChecked"
        >
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column type="index" label="序号" width="60" align="center" />
          <el-table-column prop="name" label="类型名称" min-width="280" align="center" />
          <el-table-column prop="sort" label="排序" min-width="40" align="center" />
        </el-table>
        <!-- 表格展示end-->
        <!-- 分页-->
        <pagination
          :total="total"
          :page.sync="filters.pageNum"
          :limit.sync="filters.pageSize"
          @pagination="getList"
        />
      </el-col>
    </el-row>

    <!-- 添加页面start -->
    <addDialog ref="addDialog" />
    <!-- 添加页面end-->
  </div>
</template>

<script>
// 按钮组件
import systemBtns from '@/components/Common/Btns/systemBtns'
import addDialog from './components/addDialog'
import { mapState } from 'vuex'
import {
  listCompanyTempType,
  updateCompanyTempType,
  delCompanyTempType
} from '@/api/enterprise/company-temp-type'
export default {
  components: {
    systemBtns,
    addDialog
  },
  data() {
    return {
      // 搜索框
      filters: {
        pageNum: 1,
        pageSize: this.fristPageSize,
        name: '',
        status: '0'
      },
      // 日期范围
      dateRange: [],
      statusOptions: [],
      // 选中表格对话框的存储的值
      multipleSelection: [],
      // 高亮样式
      selectRow: [],
      tableData: [],
      // 表格加载是否显示等待图标
      tableLoading: false,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 启用/停用显示状态
      disabledStatus: '0',
      disabledContent: '停用',
      // 分页
      total: 0,
      highLevelSet: false
      // listQuery: {
      //   page: 1,
      //   limit: 10,
      //   importance: undefined,
      //   title: undefined,
      //   type: undefined,
      //   sort: '+id'
      // }
    }
  },
  computed: {
    ...mapState({
      needTagsView: state => state.settings.tagsView
    }),
    // 高级搜索显示隐藏
    handleTagShow() {
      return this.highLevelSet
    }
  },
  created() {
    this.loadTableData()
    this.getDicts('sys_normal_disable').then(response => {
      this.statusOptions = response.data.rows
    })
  },
  methods: {
    // 单击每行时每行的复选框选中
    rowChecked(row) {
      // 将选中的数组赋给常量 selectData
      const selectData = this.multipleSelection
      // 调用表格的方法将所有已选的都取消
      this.$refs.multipleTable.clearSelection()
      // 判断选中的数组个数是不是等于 1
      if (selectData.length === 1) {
        // 循环判断有没有和当前行的相同的
        selectData.forEach(item => {	// 判断 如果当前的一行被勾选, 再次点击的时候就会取消选中
          if (item === row) {
            this.$refs.multipleTable.toggleRowSelection(row, false)
          } else {
            this.$refs.multipleTable.toggleRowSelection(row, true)
          }
        })
      } else { // 不等于1时
        this.$refs.multipleTable.toggleRowSelection(row, true)
      }
    },
    getList() {
      this.loadTableData()
    },
    // 表格数据
    loadTableData() {
      this.tableLoading = true
      listCompanyTempType(this.changeDateRange(this.filters, this.dateRange)).then(resp => {
        this.tableLoading = false
        if (resp) {
          this.tableData = resp.data.rows
          // this.addTaleData()
          this.total = resp.data.total
        } else {
          this.msgError(resp.message)
        }
      })
    },
    // 补空白行
    addTaleData() {
      if (this.tableData.length < this.filters.pageSize) {
        const arr = {}
        this.tableData.push(arr)
        this.addTaleData()
      }
    },
    // 点击表格前面选框时触发的事件
    handleSelectionChange(selection) {
      if (JSON.stringify(selection[0]) === '{}') {
        this.$refs.multipleTable.clearSelection()
      } else {
        this.multipleSelection = selection
        // eslint-disable-next-line no-prototype-builtins
        if (this.multipleSelection.length === 1) {
          this.disabledStatus = this.multipleSelection[0].status
          this.disabledContent = this.disabledStatus === '0' ? '停用' : '启用'
          console.log(this.disabledContent)
        }
        this.single = selection.length !== 1
        this.multiple = !selection.length
        this.selectRow = []
        if (selection.length > 0) {
          selection.forEach((item, index) => {
            this.selectRow.push(this.tableData.indexOf(item))
          })
        }
        if (selection.length > 1) {
          this.$refs.multipleTable.clearSelection()
          this.$refs.multipleTable.toggleRowSelection(selection.pop())
        }
      }
    },
    // 高亮样式
    rowClass({ row, rowIndex }) {
      if (this.selectRow.includes(rowIndex)) {
        return { 'background-color': 'rgba(185,221,249,.75)' }
      }
    },
    // 套餐类型状态修改
    handleStatusChange() {
      const row = this.multipleSelection[0]
      const text = row.status === '0' ? '停用' : '启用'
      this.$confirm('确认要' + text + '' + row.name + '套餐类型吗?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async() => {
          if (row.status === '0') {
            row.status = '1'
          } else {
            row.status = '0'
          }
          const formdata = {
            'id': row.id,
            'status': row.status
          }
          updateCompanyTempType(formdata).then(resp => {
            if (resp) {
              this.getList()
              this.msgSuccess('操作成功！')
            } else {
              this.msgError(resp.message)
            }
          })
        })
        .catch(() => {
        })
    },
    // 搜索方法
    handleQuery() {
      this.loadTableData()
    },
    // 清空搜索信息
    resetForm() {
      this.filters = {
        name: '',
        status: '',
        pageSize: this.fristPageSize

      }
      // 日期范围
      this.dateRange = []
    },
    // 重置
    resetQuery() {
      this.resetForm()
      this.loadTableData()
    },
    // 新增方法
    handleAdd() {
      this.$refs.addDialog.handleAdd('新增套餐类型')
    },
    // 编辑操作
    handleUpdate() {
      this.$refs.addDialog.handleAdd('修改套餐类型', this.multipleSelection[0])
    },
    // 删除操作
    handleDelete() {
      const child = this.multipleSelection
      this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.tableLoading = true
        let ids = ''
        for (let i = 0; i < child.length; i++) {
          ids += child[i].id + ','
        }
        // 去掉最后一个逗号
        if (ids.length > 0) {
          ids = ids.substr(0, ids.length - 1)
        }
        delCompanyTempType(ids).then(resp => {
          this.tableLoading = false
          if (resp) {
            this.msgSuccess('删除成功！')
            // 重新加载表格数据
            this.loadTableData()
          } else {
            this.msgError(resp.message)
          }
        })
      })
        .catch(() => {
        })
    }
  }
}
</script>
<style>
</style>
